package com.pucheng.bracelet.ui.activity.heartrate

import android.content.Intent
import android.os.Bundle
import androidx.activity.viewModels
import androidx.lifecycle.Observer
import com.blankj.utilcode.util.ToastUtils
import com.pucheng.bracelet.R
import com.pucheng.bracelet.app.App
import com.pucheng.bracelet.app.database.repository.HertRateRepository
import com.pucheng.bracelet.app.util.StatusBarUtil
import com.pucheng.bracelet.app.util.click
import com.pucheng.bracelet.databinding.ActivityHertrateBinding
import com.pucheng.bracelet.viewmodel.heartrate.HertRateModelFactory
import com.pucheng.bracelet.viewmodel.heartrate.HertRateViewModel
import kotlinx.android.synthetic.main.activity_hertrate.btn_start_measuring
import kotlinx.android.synthetic.main.activity_hertrate.layout_title
import kotlinx.android.synthetic.main.layout_heartrate_to_day.*
import kotlinx.android.synthetic.main.layout_title.*
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.demo.app.base.BaseActivity

class HertRateActivity : BaseActivity<BaseViewModel, ActivityHertrateBinding>() {

    private val dataViewModel: HertRateViewModel by viewModels { HertRateModelFactory(
        HertRateRepository((application as App).database.hertRateDao())
    ) }

    override fun layoutId(): Int = R.layout.activity_hertrate

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.vm = dataViewModel
        tv_title.text = getString(R.string.heartrate)
        layout_title.setBackgroundResource(R.color.heartrate_bg)
        ibtn_back.setOnClickListener { finish() }
        ibtn_right.setOnClickListener { startActivity(Intent(this,HertRateHistoryActivity::class.java)) }
        StatusBarUtil.setColor(this,resources.getColor(R.color.heartrate_bg),0)

        btn_start_measuring.click {
//            ToastUtils.showLong("开始测量")
            dataViewModel.startMeasured()
        }
    }
    override fun createObserver() {
        super.createObserver()
        dataViewModel.apply {
            hrInfo.observe(this@HertRateActivity, Observer {
                tv_heartrate.text = "${it.hertRate}BPM"
                tv_measure_date.text = it.measuredDate
            })
//            allDataTo7Day.observe(this@HertRateActivity, Observer {
//                it.let {
//
//                    tb_hint.showBarChart(it, resources.getColor(R.color.blood_pressure_bg))
//                }
//            })
        }
    }
}